//
//  PhotoSelectorViewController.swift
//  照片选择器
//
//  Created by rong on 15/7/12.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class PhotoSelectorViewController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
         
        prepareCollectionview()
    }
    
    private func prepareCollectionview() {
        
        view.addSubview(collectionView)
        collectionView.ff_Fill(view)
        collectionView.backgroundColor = UIColor.lightGrayColor()
        collectionView.registerClass(PhotoSelectorCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        collectionView.dataSource = self
        
        layout.itemSize = CGSize(width: 80, height: 80)
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    //懒加载
    private lazy var layout = UICollectionViewFlowLayout()
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.layout)
    lazy var photos = [UIImage]() // ComposeViewController 需要引用这个参数，所以不能设置为 private
}

extension PhotoSelectorViewController: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return photos.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoSelectorCell
        
        //cell.backgroundColor = UIColor.blueColor()
        cell.SelectPhotoDelegate = self
        cell.image = (indexPath.item == photos.count) ? nil : photos[indexPath.item]
        
        return cell
    }
}

//点击cell里面的图片的 代理协议
extension PhotoSelectorViewController: PhotoSelectorCellDelegate {
    
    //添加照片
    func PhotoSelectorCellSelectPhoto() {
        
        print(__FUNCTION__)
        
       // UIImagePickerController 专门用来选择照片的
       /*
            PhotoLibrary  照片库（所有的照片，拍照的  以及  iTunes和iphoto 同步的照片－－不能删除）
            SavedPhotosAlbum  相册（自己拍摄的，可以随意删除）
            Camera        相机
        */
        //看是否支持 照片库
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            return
        }
        
        let picker = UIImagePickerController()
        
        picker.delegate = self
        
        //设置代理
        
        presentViewController(picker, animated: true, completion: nil)
    }
    
    //删除照片
    func PhotoSelectorCellDeletePhoto(cell: PhotoSelectorCell) {
    
        let indexPath = collectionView.indexPathForCell(cell)
        
        photos.removeAtIndex(indexPath!.item)
        
        collectionView.reloadData()
    }

}


extension PhotoSelectorViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        //获得图像
        print(image)
        
        //将图像添加到数组中
        photos.append(image.scaleImage(300))
        
        collectionView.reloadData()
        
        //一旦自己实现这个协议方法，就必须自己关闭相片查看器
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    
}


















